package cn.stylefeng.guns.modular.gridsystem.controller;

import cn.stylefeng.guns.core.annotion.BusinessLog;
import cn.stylefeng.guns.core.enums.LogAnnotionOpTypeEnum;
import cn.stylefeng.guns.core.pojo.response.ResponseData;
import cn.stylefeng.guns.core.pojo.response.SuccessResponseData;
import cn.stylefeng.guns.modular.gridsystem.entity.BizCollectorConfig;
import cn.stylefeng.guns.modular.gridsystem.entity.BizGateway;
import cn.stylefeng.guns.modular.gridsystem.model.param.BizCollectorConfigParam;
import cn.stylefeng.guns.modular.gridsystem.model.param.BizGatewayParam;
import cn.stylefeng.guns.modular.gridsystem.model.result.GatewayResult;
import cn.stylefeng.guns.modular.gridsystem.service.BizCollectorConfigService;
import cn.stylefeng.guns.modular.gridsystem.service.BizGatewayService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import cn.stylefeng.guns.modular.gridsystem.service.BizCollectorConfigTemplateService;
import cn.stylefeng.guns.modular.gridsystem.model.param.BizCollectorConfigTemplateParam;

import javax.annotation.Resource;
import java.util.List;

/**
 * 采集器配置模板 控制器
 *
 * @author: ssy
 * @date: 2022/02/19 12:05
 */
@RestController
public class BizCollectorConfigTemplateController {

    @Resource
    private BizGatewayService bizGatewayService;

    @Resource
    private BizCollectorConfigService bizCollectorConfigService;

    @Resource
    private BizCollectorConfigTemplateService bizCollectorConfigTemplateService;

    /**
     * 分页查询采集器配置模板
     *
     * @author ssy
     * @date 2022/02/19 12:05
     */
    @GetMapping("/bizCollectorConfigTemplate/page")
    @BusinessLog(title = "采集器配置模板_分页查询", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData page(BizCollectorConfigTemplateParam bizCollectorConfigTemplateParam) {
        return new SuccessResponseData(bizCollectorConfigTemplateService.page(bizCollectorConfigTemplateParam));
    }

    /**
     * 获取全部采集器配置模板
     *
     * @author ssy
     * @date 2022/02/19 12:05
     */
    @GetMapping("/bizCollectorConfigTemplate/list")
    @BusinessLog(title = "采集器配置模板_查询所有", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData list(BizCollectorConfigTemplateParam bizCollectorConfigTemplateParam) {
        return new SuccessResponseData(bizCollectorConfigTemplateService.list(bizCollectorConfigTemplateParam));
    }

    /**
     * 查看详情采集器配置模板
     *
     * @author ssy
     * @date 2022/02/19 12:05
     */
    @GetMapping("/bizCollectorConfigTemplate/detail")
    @BusinessLog(title = "采集器配置模板_查看详情", opType = LogAnnotionOpTypeEnum.DETAIL)
    public ResponseData detail(@Validated(BizCollectorConfigTemplateParam.detail.class) BizCollectorConfigTemplateParam bizCollectorConfigTemplateParam) {
        return new SuccessResponseData(bizCollectorConfigTemplateService.detail(bizCollectorConfigTemplateParam));
    }

    /**
     * 添加采集器配置模板
     *
     * @author ssy
     * @date 2022/02/19 12:05
     */
    @PostMapping("/bizCollectorConfigTemplate/add")
    @BusinessLog(title = "采集器配置模板_增加", opType = LogAnnotionOpTypeEnum.ADD)
    @Transactional(rollbackFor = Exception.class)
    public ResponseData add(@RequestBody @Validated(BizCollectorConfigTemplateParam.add.class) BizCollectorConfigTemplateParam bizCollectorConfigTemplateParam) {
        bizCollectorConfigTemplateService.add(bizCollectorConfigTemplateParam);
        BizGatewayParam bizGatewayParam = new BizGatewayParam();
        bizGatewayParam.setTypeCode(bizCollectorConfigTemplateParam.getGatewayTypeCode());
        List<GatewayResult> list = bizGatewayService.list(bizGatewayParam);
        for (GatewayResult bizGateway : list) {
            Integer gatewayId = bizGateway.getId();
            BizCollectorConfigParam bizCollectorConfigParam = new BizCollectorConfigParam();
            bizCollectorConfigParam.setGatewayId(gatewayId);
            bizCollectorConfigParam.setGatewayTypeCode(bizCollectorConfigTemplateParam.getGatewayTypeCode());
            bizCollectorConfigParam.setCode(bizCollectorConfigTemplateParam.getCode());
            bizCollectorConfigParam.setValue(bizCollectorConfigTemplateParam.getValue());
            bizCollectorConfigParam.setRemark(bizCollectorConfigTemplateParam.getRemark());
            bizCollectorConfigParam.setConfigName(bizCollectorConfigTemplateParam.getConfigName());
            bizCollectorConfigParam.setStatus(bizCollectorConfigTemplateParam.getStatus());
            bizCollectorConfigService.add(bizCollectorConfigParam);

        }
        return new SuccessResponseData();
    }

    /**
     * 删除采集器配置模板
     *
     * @author ssy
     * @date 2022/02/19 12:05
     */
    @PostMapping("/bizCollectorConfigTemplate/delete")
    @BusinessLog(title = "采集器配置模板_删除", opType = LogAnnotionOpTypeEnum.DELETE)
    @Transactional(rollbackFor = Exception.class)
    public ResponseData delete(@RequestBody @Validated(BizCollectorConfigTemplateParam.delete.class) BizCollectorConfigTemplateParam bizCollectorConfigTemplateParam) {
        bizCollectorConfigTemplateService.delete(bizCollectorConfigTemplateParam);
        BizCollectorConfigParam bizCollectorConfigParam = new BizCollectorConfigParam();
        bizCollectorConfigParam.setCode(bizCollectorConfigTemplateParam.getCode());
        bizCollectorConfigParam.setGatewayTypeCode(bizCollectorConfigTemplateParam.getGatewayTypeCode());
        List<BizCollectorConfig> list = bizCollectorConfigService.list(bizCollectorConfigParam);
        for (BizCollectorConfig bizCollectorConfig : list) {
            bizCollectorConfigService.removeById(bizCollectorConfig.getId());
        }
        return new SuccessResponseData();
    }

    /**
     * 编辑采集器配置模板
     *
     * @author ssy
     * @date 2022/02/19 12:05
     */
    @PostMapping("/bizCollectorConfigTemplate/edit")
    @BusinessLog(title = "采集器配置模板_编辑", opType = LogAnnotionOpTypeEnum.EDIT)
    public ResponseData edit(@RequestBody @Validated(BizCollectorConfigTemplateParam.edit.class) BizCollectorConfigTemplateParam bizCollectorConfigTemplateParam) {
        bizCollectorConfigTemplateService.edit(bizCollectorConfigTemplateParam);
        return new SuccessResponseData();
    }

    /**
     * 修改状态
     *
     * @author ssy
     * @date 2022/02/19 12:05
     */
    @PostMapping("/bizCollectorConfigTemplate/changeStatus")
    @BusinessLog(title = "采集器配置模板_修改状态", opType = LogAnnotionOpTypeEnum.CHANGE_STATUS)
    public ResponseData changeStatus(@RequestBody @Validated(BizCollectorConfigTemplateParam.changeStatus.class) BizCollectorConfigTemplateParam bizCollectorConfigTemplateParam) {
        bizCollectorConfigTemplateService.changeStatus(bizCollectorConfigTemplateParam);
        return new SuccessResponseData();
    }

}
